var Fiber = require("fibers");

Array.prototype.list = function()
{
    var args = arguments;
    var obj = {};
    for(var i=0; i<args.length; i++){
        if(this[i]===undefined){
            obj[args[i]] = undefined;
        }else{
            obj[args[i]] = this[i];
        }
    }
    return obj;
};

module.exports = {

    fibersMiddleWare: function (req, res, next) {
        Fiber(function () {
            next();
        }).run();
    },

    fibersInvoke: function () {
        var argsArray = Array.prototype.slice.call(arguments);

        var thisArg = argsArray[0];
        var fn = argsArray[1];
        var fiber = Fiber.current;
        var returnArrays;

        argsArray.shift();
        argsArray.shift();
        argsArray.push(function () {
            returnArrays = Array.prototype.slice.call(arguments);
            fiber.run();
        });

        fn.apply(thisArg, argsArray);
        Fiber.yield();
        return returnArrays;
    }

};